/**
 * @author Andrew Dashin <dashin@andrewdashin.com>
 * 
 * This is my AJAX implementation.
 * Everyone already made its own implementation.
 * And me too! :)
 */
var Ajax = Class.create();
Ajax.prototype = Object.extend({}, {
    initialize: function (sUrl, oRequest) {
        if (typeof(sUrl) == 'undefined' || typeof(oRequest) == 'undefined')
            return false;
        this.transport = this.getTransport();
        if (this.transport) {

            var sParameters = '';
            if (oRequest.parameters != '')
            for (var property in oRequest.parameters)
                sParameters += property + '=' + oRequest.parameters[property] + '&';

            if (oRequest.method == 'get' && sParameters != '')
                sUrl += '?' + sParameters;
            this.transport.open(oRequest.method, sUrl, true);
            this.request = oRequest;
            
            var oObj = this;
            this.transport.onreadystatechange = function() { oObj.handler(oObj); };
            this.setRequestHeaders(oRequest);
            this.transport.send(oRequest.method == 'post' && sParameters != '' ? sParameters : null);
        } else {
            return false;
        }
    },
    setRequestHeaders: function (oRequest) {
        if (oRequest.method == 'post')
          this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        
    },
    getTransport: function () {
        try {
            return new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
            try {
                return new ActiveXObject('Microsoft.XMLHTTP')
            } catch (e) {
                try {
                    return new XMLHttpRequest();
                } catch (e) {
                    return false;
                }
            }
        }        
    },
    handler: function (oObj) {
        switch (oObj.transport.readyState) {
            case 0: 
                // not initialized
                break;
            case 1: 
                // opened
                if (oObj.request && oObj.request.onOpen)
                    oObj.request.onOpen(oObj);
                break;
            case 2: 
                // sending data
                if (oObj.request && oObj.request.onSend)
                    oObj.request.onSend(oObj);
                break;
            case 3: 
                // recieving data
                if (oObj.request && oObj.request.onRecieve)
                    oObj.request.onRecieve(oObj);
                break;
            case 4: 
                // data loaded
                if (oObj.request && oObj.request.onResponseRecieved)
                    if (oObj.transport.status == 200) {
                        oObj.request.onResponseRecieved(oObj);
                    } else {
                        if (oObj.request.onFailure)
                            oObj.request.onFailure(oObj);
                    }
                    
                break;
        }
    }
});

var Service = Class.create();
Service.prototype = Object.extend(new Ajax(), {
    initialize: function (oRequest) {
        if (typeof(oRequest) == 'undefined')
            return false;
        oRequest.method = 'post';
        Ajax.prototype.initialize.call(this, 'index.php', oRequest);
    }
});
